package com.heima.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.redis.CacheService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 敏感词服务
 *
 * @author 柳问风
 * @date 2023/08/02
 */
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {
    @Autowired
    private CacheService cacheService;

    /**
     * 分页查询
     *
     * @param sensitiveDto
     * @return
     */
    @Override
    public ResponseResult list(SensitiveDto sensitiveDto) {
        if (sensitiveDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        sensitiveDto.checkParam();
        //分页查询
        IPage ipage = new Page(sensitiveDto.getPage(), sensitiveDto.getSize());
        LambdaQueryWrapper<WmSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //模糊查询
        if (StringUtils.isNotBlank(sensitiveDto.getName())) {
            lambdaQueryWrapper.like(WmSensitive::getSensitives, sensitiveDto.getName());
        }

        ipage = page(ipage, lambdaQueryWrapper);

        //返回
        ResponseResult result = new PageResponseResult(sensitiveDto.getPage(), sensitiveDto.getSize(), (int) ipage.getTotal());
        result.setData(ipage.getRecords());
        //同步到redis
//        //1.先删除redis中的缓存
//        cacheService.delete("sensitive");
//
//        List<WmSensitive> list = list(lambdaQueryWrapper);
//
//        for (WmSensitive wmSensitive : list) {
//            cacheService.lLeftPush("sensitive",wmSensitive.getSensitives());
//        }
        return result;
    }

    /**
     * 删除
     *
     * @param id id
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult delete(Integer id) {
        //参数校验
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询敏感词是否存在
        WmSensitive sensitive = getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getId, id));
        if (sensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "敏感词不存在");
        }
        //删除敏感词
        removeById(id);
        cacheService.lRemove("sensitive", 0, sensitive.getSensitives());
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 添加
     *
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult insert(WmSensitive wmSensitive) {
        //校验参数
        if (wmSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询是否存在
        WmSensitive sensitive = getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));
        if (sensitive != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "敏感词已存在");
        }
        //设置创建时间
        wmSensitive.setCreatedTime(new Date());
        //保存
        save(wmSensitive);

        cacheService.lLeftPush("sensitive", wmSensitive.getSensitives());
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改
     *
     * @param wmSensitive
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult update(WmSensitive wmSensitive) {
        //校验参数
        if (wmSensitive == null || wmSensitive.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询敏感词是否存在
        WmSensitive sensitive = getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getId, wmSensitive.getId()));
        if (sensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "敏感词不存在");
        }
        int count = count(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));
        if (count >0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "敏感词重复");
        }
        //删除rides中相应的敏感词
        cacheService.lRemove("sensitive", 0, sensitive.getSensitives());
        //修改
        updateById(wmSensitive);
        //在rides保存敏感词
        cacheService.lLeftPush("sensitive", wmSensitive.getSensitives());
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
